package com.tongren.framework.sample;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tongren.framework.R;
import com.tongren.framework.api.Api;
import com.tongren.framework.core.BaseActivity;
import com.tongren.framework.core.BaseListActivity;
import com.tongren.framework.model.BaseModel;
import com.tongren.framework.model.Benefit;
import com.tongren.framework.utils.L;
import com.tongren.framework.widgets.pull.BaseViewHolder;
import com.tongren.framework.widgets.pull.PullRecycler;

import java.util.ArrayList;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 我的作业
 */
public class SampleListActivity1 extends BaseListActivity<Benefit> {

    private int page = 1;

    @Override
    protected void setUpTitle(int titleResId) {
        super.setUpTitle(R.string.title_recycler_fragment);
    }

    @Override
    protected void setUpData(Bundle savedInstanceState) {
        super.setUpData(savedInstanceState);
        recycler.setRefreshing();
    }

    @Override
    protected BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_list_item, parent, false);
        return new SampleListActivity1.SampleViewHolder(view);
    }

    @Override
    public void onRefresh(final int action) {
        if (mDataList == null) {
            mDataList = new ArrayList<>();
        }

        if (action == PullRecycler.ACTION_PULL_TO_REFRESH) {
            page = 1;
        }
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://gank.io/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        Api api = retrofit.create(Api.class);
        api.rxBenefits(20, page++)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseModel<ArrayList<Benefit>>>() {
                    @Override
                    public void call(BaseModel<ArrayList<Benefit>> model) {
                        if (action == PullRecycler.ACTION_PULL_TO_REFRESH) {
                            mDataList.clear();
                        }
                        if (model.results == null || model.results.size() == 0) {
                            recycler.enableLoadMore(false);
                        } else {
                            recycler.enableLoadMore(true);
                            mDataList.addAll(model.results);
                            adapter.notifyDataSetChanged();
                        }
                        recycler.onRefreshCompleted();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        recycler.onRefreshCompleted();
                    }
                });

    }

    class SampleViewHolder extends BaseViewHolder {

//        ImageView iv_new_img;
        TextView tv_new_title;
        TextView tv_new_date;

        public SampleViewHolder(View itemView) {
            super(itemView);
            tv_new_title = (TextView) itemView.findViewById(R.id.tv_new_title);
            tv_new_date = (TextView) itemView.findViewById(R.id.tv_new_date);
//            iv_new_img = (ImageView) itemView.findViewById(R.id.iv_new_img);
        }

        @Override
        public void onBindViewHolder(int position) {
//            Glide.with(iv_new_img.getContext())
//                    .load(mDataList.get(position).url)
//                    .centerCrop()
//                    .placeholder(R.color.app_primary_color)
//                    .crossFade()
//                    .into(iv_new_img);
        }

        @Override
        public void onItemClick(View view, int position) {

        }

    }
}
